package com.link.base.base.expenseapplication.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.link.base.base.expenseapplication.model.ApplicationHead;
import com.link.base.base.expenseapplication.model.ApplicationHeadInfo;
import com.link.base.base.expenseapplication.service.ApplicationHeadService;
import com.link.base.core.basic.aop.AuditTrailAnnotation;
import com.link.base.core.basic.config.mvc.CustomerLongTypeMapper;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 费用申请头-费用申请头
 *
 * @author 黄正杨
 * @date 2019/04/16 19:06
 */
@Controller
@RequestMapping("/link/applicationHead")
public class ApplicationHeadController extends BasicController<ApplicationHead> {


    @Resource
    private ApplicationHeadService applicationHeadService;

    @Override
    public BasicService<ApplicationHead> getBasicService() throws Exception {
        return applicationHeadService;
    }

    /**
     * 提交申请，创建审批流程
     */
    @RequestMapping(value = "/submit")
    @ResponseBody
    @AuditTrailAnnotation
    public Map<String, Object> submit(@RequestBody ApplicationHeadInfo applicationHeadInfo,
                                      HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(8);
        ObjectMapper objectMapper = new CustomerLongTypeMapper();
        try {
            String applicationHeadDetail = objectMapper.writeValueAsString(applicationHeadInfo);
            if (null == applicationHeadInfo.getApplicationHead().getId()) {
                throw new ServiceException("PUBLIC-007");
            }
            applicationHeadService.submit(applicationHeadInfo.getApplicationHead(), applicationHeadDetail);
            ApplicationHead newTra = applicationHeadService.queryById(applicationHeadInfo.getApplicationHead());
            result.put("newRow", newTra);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 审批处理-审批成功后更新信息
     *
     * @param entity
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/applicationStatusUpdate", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> applicationStatusUpdate(@RequestBody ApplicationHeadInfo entity, HttpSession session,
                                                        HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            /*更新信息*/
            applicationHeadService.applicationObjectUpdate(entity);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

}
